<?php

class IndexController extends Yaf_Controller_Abstract
{

    private $adminModel;
    private $caseModel;
    private $sessoin;
    private $PAGESIZE = 50;
    public function init()
    {
        $this->caseModel = new CaseModel();
        $this->adminModel = new AdminModel();
        $this->session = Yaf_Session::getInstance();
    }
    public function indexAction()
    {
        if (!isset($this->session['user'])){
            $this->forward('login');
            return FALSE;
        }
    }
    public function listAction()
    {
        if (isset($this->session['user']))
        {
            $page = intval($this->getRequest()->getParam('page'));
            $release = intval($this->getRequest()->getParam('state'));
            $page = ($page == 0) ? 1 : $page;
            $offset = ($page-1) * $this->PAGESIZE;
            if ($release == 1)
                $info = $this->infoModel->fetchInfo($offset,$this->PAGESIZE);
            else
                $info = $this->infoModel->fetchPendingInfo($offset,$this->PAGESIZE);
            $this->getView()->assign('info',$info);
        }
        else{
            $this->forward('login');
            return FALSE;
        }
    }
    public function userAction()
    {
        $username = $this->getRequest()->getPost('username');
        $password = md5($this->getRequest()->getPost('password'));
        if ($username!='' && $password!='')
        {
            $state = $this->adminModel->login($username,$password);
            $session = Yaf_Session::getInstance();
            if ($state){
                $session['user'] = $username;
                $flag = true;
            }
        }
        if ($flag)
            $this->forward('index');
        else
            $this->forward('login');
        return FALSE;
    }
    public function removeInfoAction()
    {
        if (isset($this->session['user']))
        {
            $id = $this->getRequest()->getParam('id');
            if ($id != '')
                $this->caseModel->updateInfo(array('publish_status'=>0),array('id'=>$id));
            $this->forward('list',array('state'=>1));
        }
        else
            $this->forward('login');
        return FALSE;
    }
    public function releaseInfoAction()
    {
        if (isset($this->session['user']))
        {
            $id = $this->getRequest()->getParam('id');
            if ($id != '')
                $this->caseModel->updateInfo(array('publish_status'=>1),array('id'=>$id));
            $this->forward('list');
        }
        else
            $this->forward('login');
        return FALSE;
    }
    public function loginAction()
    {
    }
    public function logoutAction()
    {
        unset($this->session['user']);
        $this->forward('login');
        return FALSE;
    }
}
?>
